package com.fjh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjh.domain.ProdProp;
import com.fjh.domain.ProdPropValue;
import com.fjh.service.ProdPropService;
import com.fjh.service.ProdPropValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("prod/spec")
@Api(tags = "属性管理")
public class PropController {

    @Autowired
    private ProdPropService prodPropService;

    @Autowired
    private ProdPropValueService prodPropValueService;

    @GetMapping("page")
    @ApiOperation("分页查询属性和属性值")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public ResponseEntity<Page<ProdProp>> loadProdpropPage(Page<ProdProp>page, ProdProp prodProp){
        Page<ProdProp> prodPropPage=prodPropService.loadProdPropPage(page,prodProp);
        return ResponseEntity.ok(prodPropPage);
    }

    @PostMapping
    @ApiOperation("新增属性和属性值")
    @PreAuthorize("hasAuthority('prod:spec:save')")
    public ResponseEntity<Void> saveProdProp(@RequestBody ProdProp prodProp){
        prodPropService.save(prodProp);
        return ResponseEntity.ok().build();
    }

    @GetMapping("list")
    @ApiOperation("全查询属性集合")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public ResponseEntity<List<ProdProp>> loadAllProdProp(){
        List<ProdProp> list = prodPropService.list();
        return ResponseEntity.ok(list);
    }

    @GetMapping("listSpecValue/{id}")
    @ApiOperation("根据属性的id查询属性值集合")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public ResponseEntity<List<ProdPropValue>> listSpecValue(@PathVariable("id") long id){
        List<ProdPropValue> prodPropValues = prodPropValueService.findPropValueByPropId(id);
        return ResponseEntity.ok(prodPropValues);
    }

}
